<template>
  <div
    class="wrap-item"
    :style="{
      '--scroll-width': '175px',
    }"
  >
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model="formSearch.param.orderName"
          maxlength="20"
          placeholder="请输入工单名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="formSearch.param.title"
          maxlength="50"
          placeholder="请输入工单标题"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-cascader
          style="width: 100%"
          v-model="formSearch.param.typeId"
          :options="typeData"
          @change="handleChange"
          :props="{
            value: 'id',
            label: 'typeName',
            children: 'childList',
            expandTrigger: 'hover',
            emitPath: false,
            checkStrictly: true,
          }"
          :checkStrictly="true"
          :show-all-levels="false"
          placeholder="请选择工单类型"
          ref="typeCascader"
          clearable
          popper-class="theder"
        >
        </el-cascader>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="formSearch.param.date"
          @change="dateChange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始创建时间"
          end-placeholder="结束创建时间"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-other-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'childList', hasChildren: 'hasChildren' }"
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column prop="orderName" label="工单名称"></el-table-column>
        <el-table-column prop="title" label="工单标题">
          <template slot-scope="scope">
            <el-tooltip
              popper-class="ellipsis-tip"
              :content="scope.row.title"
              placement="top-start"
            >
              <span class="ellipsis">{{ scope.row.title }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="typeName" label="工单类型"></el-table-column>
        <el-table-column prop="expireDataObj" label="处理时限">
          <template slot-scope="scope">
            <span v-if="scope.row.expireDataObj.type == 0"
              >{{ scope.row.expireDataObj.expire }}天</span
            >
            <span v-if="scope.row.expireDataObj.type == 1"
              >{{ scope.row.expireDataObj.expire }}小时</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="submitUserName" label="联系人"></el-table-column>
        <el-table-column
          prop="submitUserPhone"
          label="联系电话"
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="150px"
        ></el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item command="assign" v-if="$checkPermission(114)"
                  >指派</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
    <!-- 查看 -->
    <detail-item :visible="isLook" :data="lookData" @close="isLook = false" />
    <!-- 人员对话框 -->
    <user-panel-plus
      :visible="visible"
      :data="data"
      :defaultProps="{
        key: 'id',
        defaultParentId: '0',
        parentId: 'parentId',
        id: 'userId',
        name: 'name',
        children: 'children',
      }"
      @close="visible = false"
      @save="save"
    />
  </div>
</template>

<script>
import detailItem from './../components/detail'
export default {
  components: { detailItem },
  data() {
    return {
      typeData: [],
      data: [],
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: { status: 0 },
      },
      total: null,
      tableData: [],
      visible: false,
      form: { userLists: [] },
      lookData: {},
      isLook: false,
    }
  },
  created() {
    this.typeList()
    this.organizationList()
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios
        .post('/workorder/myToDoAppoint/myToDoAppointPage', this.formSearch)
        .then((data) => {
          this.tableData = []
          if (data.code == 200) {
            this.tableData = data.data.list
            this.total = data.data.total
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          //查看
          this.lookData = {
            workType: row.workType,
            id: row.id,
          }
          this.isLook = true
          break
        case 'assign':
          this.form.workOrderId = row.id
          this.visible = true
          break
      }
    },
    save(data) {
      //保存
      this.form.userLists = []
      data.forEach((element) => {
        if (element.userId != 0) {
          this.form.userLists.push({
            appointUserId: element.userId,
            appointUserName: element.name,
          })
        }
      })
      this.$axios
        .post('/workorder/workOrder/workOrderMonitoForward', this.form)
        .then((data) => {
          if (data.code == 200) {
            this.visible = false
            this.itemList()
            this.$message.success('操作成功！')
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    dateChange(val) {
      if (val) {
        this.formSearch.param.startTime = val[0]
        this.formSearch.param.endTime = val[1]
      } else {
        this.formSearch.param.startTime = ''
        this.formSearch.param.endTime = ''
      }
    },
    typeList() {
      // 获取工单类型
      this.$axios
        .post('/workorder/configWorkOrderTypeManage/workTypeList')
        .then((data) => {
          this.typeData = []
          if (data.code == 200) {
            this.typeData = data.data
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    organizationList() {
      // 获取组织人员
      this.$axios.get('/admin/org/listUnitTreeV2').then((data) => {
        this.data = []
        if (data.code == 200) {
          this.data = data.data
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    handleChange(val) {
      this.$refs.typeCascader.dropDownVisible = false //监听值发生变化就关闭它
    },
  },
}
</script>
